﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSISDynamicTranslation;
using OSISToolTip;
using System.Data;
using OSISFunction;
using OpenSIS.OpenSISWCFServiceReference;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;

namespace OpenSIS.Student
{
    public partial class StudentSetup : System.Web.UI.Page
    {   
        static int AddSet = 1;
        int InitialFieldId = 0;
        TooltipLang TTL = new TooltipLang();
        DynamicTranslation DTL = new DynamicTranslation();
        public static string langto;

        protected void Page_Load(object sender, EventArgs e)
        {
            string sCulture = Session["MLCulture"].ToString();
            langto = Session["MLUICulture"].ToString();
            PopulateMLUI();
            PopulateCategoryTree();
            //TreeView1.Nodes[0].Select();
            TTL.tooltip();
            ibtnAdd.Attributes.Add("onmouseover", "toolTip('" + TTL.Add + "')");
            ibtnAdd.Attributes.Add("onmouseout", "toolTip()");
            ibtnAdd.Attributes.Add("onclick", "toolTip()");
            imgbtnEdit.Attributes.Add("onmouseover", "toolTip('" + TTL.Edit + "')");
            imgbtnEdit.Attributes.Add("onmouseout", "toolTip()");
            ibtnEditCategory.Attributes.Add("onmouseover", "toolTip('" + TTL.Edit + "')");
            ibtnEditCategory.Attributes.Add("onmouseout", "toolTip()");
            ibtnDeleteCategory.Attributes.Add("onmouseover", "toolTip('" + TTL.Delete + "')");
            ibtnDeleteCategory.Attributes.Add("onmouseout", "toolTip()");  
            imgbtnDelete.Attributes.Add("onmouseover", "toolTip('" + TTL.Delete + "')");
            imgbtnDelete.Attributes.Add("onmouseout", "toolTip()");
            imgbtnDelete.Attributes.Add("onclick", "return GridCheckBoxSelection('" + sCulture + "')");
            if (hdnCatId.Value.ToString() == "8")
            {
                ibtnAdd.Attributes.Add("onclick", "return GridCheckBoxSelectionsetup('" + sCulture + "')");
            }
            if (!Page.IsPostBack)
            {
                populateusercontrols();
                //populateStudentSetupCategory();
                TreeView1.Nodes[0].Select();
                hdnCatId.Value = TreeView1.SelectedNode.Value.Split(",".ToCharArray())[0];
                hdnNode.Value = TreeView1.SelectedNode.ValuePath;
                PopulateSchoolCategory();
            }
            ddlFieldType.Attributes.Add("onChange", "return ddlFieldTypeOnSelectedIndexChange('" + panellist.ClientID + "','" + lblmsg.ClientID + "','" + lblmsgCtrl.ClientID + "','" + txtListitems.ClientID + "','" + btnSaveControlListItems.ClientID + "','" + TableListItem.ClientID + "');");
            imgbtnEdit.Attributes.Add("onclick", "return GridCheckBoxSelectionsetup('" + sCulture + "')");
            
        }
        
        public void PopulateCategoryTree()
        {
            try
            {   
                string SchoolID = Session["SchoolID"].ToString();
                OpenSISWCFServiceStudentSetupClient OSISStudentClient = new OpenSISWCFServiceStudentSetupClient();
                DataTable dt = OSISStudentClient.GetSchoolSpecificCategory(SchoolID, Session["MLUICulture"].ToString());
                if (OSISStudentClient.State == CommunicationState.Opened)
                {
                    OSISStudentClient.Close();
                }
                TreeView1.Nodes.Clear();
                string[,] YearNodes = new string[100, 3];
                for (int i = 1; i < dt.Rows.Count + 1; i++)
                {
                    TreeNode TreeNodeCategory = new TreeNode();
                    //if (i == 0)
                    //{
                    //    YearNodes[i, 0] = "+ Add Category&nbsp;&nbsp;&nbsp;&nbsp;";
                    //    YearNodes[i, 1] = "0";
                    //    YearNodes[i, 2] = "0";
                    //}
                    //else
                    //{
                        YearNodes[i, 0] = dt.Rows[i-1]["Cat_Name"].ToString();
                        YearNodes[i, 1] = dt.Rows[i-1]["Cat_Id"].ToString();
                        YearNodes[i, 2] = dt.Rows[i-1]["Short_Order"].ToString();
                    //}
                    if(i==1)
                    {
                        hdnCatOrder.Value = YearNodes[i, 2];
                    }

                    TreeNodeCategory.Text = YearNodes[i, 0];
                    TreeNodeCategory.Value = YearNodes[i, 1] + "," + i + "," + YearNodes[i, 2];
                    TreeView1.Nodes.Add(TreeNodeCategory);
                    TreeView1.ExpandAll();
                }
                if (hdnNode.Value != "")
                {
                    TreeView1.FindNode(hdnNode.Value.ToString()).Select();
                }
               
            }
            catch (FaultException ex) 
            {
                string msg = "FaultException: " + ex.Message;
                MessageFault fault = ex.CreateMessageFault();
                if (fault.HasDetail == true)
                {
                    System.Xml.XmlReader reader = fault.GetReaderAtDetailContents();
                    if (reader.Name == "ExceptionDetail")
                    {
                        ExceptionDetail detail = fault.GetDetail<ExceptionDetail>();
                        msg += "\n\nStack Trace: " + detail.StackTrace;
                    }
                }              
               Response.Write(msg);
            }
        }

        protected void populateusercontrols()
        {
            Function Func = new Function();
            try
            {
                Func.populateUserControls(ref ddlFieldType);
            }
            catch { }
            finally
            {
                Func = null;
            }
        }

        //protected void populateStudentSetupCategory()
        //{
        //    Function stsuBL = new Function();
        //    try
        //    {
        //        stsuBL.populateCategory(ref ddlCategory, Convert.ToInt32(Session["SchoolID"]), Session["MLUICulture"].ToString());
        //    }
        //    catch { }
        //    finally
        //    {
        //        stsuBL = null;
        //    }
        //}

        protected void PopulateSchoolCategory()
        {
            OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
            try
            {
                int CategoryId = Convert.ToInt32(hdnCatId.Value);                
                int SchoolId = Convert.ToInt32(Session["SchoolID"]);
                DataTable dt = schoolcategory.GetFieldsByCategory(CategoryId,SchoolId,Session["MLUICulture"].ToString());
                schoolcategory.Close();
                if (hdnCatId.Value == "3")
                {
                    //DataTable dtadd = new DataTable();
                    DataRow dr = null;
                    //dt.Columns.Add("WeekEnd_Day");
                    dr = dt.NewRow();
                    dr["FieldName"] = "Additional Contact";
                    dr["FieldId"] = "900";
                    dr["FieldType"] = "6";
                    dr["Status"] = "A";
                    dr["Default_Custom"] = "AD";
                    dr["IsMandatory"] = "M";
                    dr["Field_View"] = "V_E";
                    dt.Rows.Add(dr);
                }
                gvCategoryFields.DataSource = dt;
                gvCategoryFields.DataBind();
                lblHeader.Text = TreeView1.SelectedNode.Text.ToString();
                lblShortOrder.Text = TreeView1.SelectedNode.Value.Split(",".ToCharArray())[2];
            }
            catch { }
            finally
            {
               // schoolcategory = null;
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {          
            OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
            try
            {   
                hdnNode.Value = TreeView1.SelectedNode.ValuePath;
                hdnCatId.Value = TreeView1.SelectedNode.Value.Split(",".ToCharArray())[0];
                hdnCatOrder.Value = TreeView1.SelectedNode.Value.Split(",".ToCharArray())[2];
                Session["SelectIndex"] = TreeView1.SelectedNode.Value;
                Session["CatSrtOrd"] = hdnCatOrder.Value;
                string sCulture = Session["MLCulture"].ToString();

                if (hdnCatId.Value.ToString() == "8")
                {
                    ibtnAdd.Attributes.Add("onclick", "return GridCheckBoxSelectionsetup('" + sCulture + "')");
                }

                    int CategoryId = Convert.ToInt32(hdnCatId.Value);

                    if (CategoryId == 3)
                    {
                        ibtnAdd.Visible = false;
                        imgSeperator1.Visible = false;
                    }
                    else
                    {
                        ibtnAdd.Visible = true;
                        imgSeperator1.Visible = true;
                    }

                    if (CategoryId == 8)
                    {
                        ltrlAddcat.Visible = true;
                        lblcolon.Visible = true;
                        lblHeaderText.Visible = true;
                    }
                    else
                    {
                        ltrlAddcat.Visible = false;
                        lblcolon.Visible = false;
                        lblHeaderText.Visible = false;
                    }
                    
                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
                    DataTable dt = schoolcategory.GetFieldsByCategory(CategoryId, SchoolId, Session["MLUICulture"].ToString());
                    if (hdnCatId.Value == "3" || hdnCategoryId.Value == "3")
                    {
                        DataRow dr = null;
                        dr = dt.NewRow();
                        dr["FieldName"] = "Additional Contact";
                        dr["FieldId"] = "900";
                        dr["FieldType"] = "0";
                        dr["Status"] = "A";
                        dr["Default_Custom"] = "AD";
                        dr["IsMandatory"] = "M";
                        dr["Field_View"] = "V_E";
                        dt.Rows.Add(dr);
                    }
                    gvCategoryFields.DataSource = dt;
                    gvCategoryFields.DataBind();
                    lblHeader.Text = TreeView1.SelectedNode.Text.ToString();
                    lblShortOrder.Text = hdnCatOrder.Value;
            }
            catch (Exception x)
            {
                string s = x.Message;
            }
            finally
            {
                if (schoolcategory.State == CommunicationState.Opened)
                {
                    schoolcategory.Close();
                }                      
            }
        }       

        protected void btnSaveControlListItems_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            try
            {
                dt.Columns.Add("ListItem");
                dt.Columns.Add("FieldType");
                for (int i = 0; i < gvControlListItem.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    Label lb1 = (Label)gvControlListItem.Rows[i].FindControl("lblddllistitem");
                    //HiddenField hdnfieldType = (HiddenField)gvControlListItem.Rows[i].FindControl("hdnfieldType");
                    //if (hdnfieldType.Value == ddlFieldType.SelectedItem.Text)
                    //{
                        dr["ListItem"] = lb1.Text;
                        dr["FieldType"] = ddlFieldType.SelectedItem.Text;
                        dt.Rows.Add(dr);
                    //}
                }
                DataRow dr1 = dt.NewRow();
                dr1["ListItem"] = txtListitems.Text;
                dr1["FieldType"] = ddlFieldType.SelectedItem.Text;
                dt.Rows.Add(dr1);
                gvControlListItem.DataSource = dt;
                gvControlListItem.DataBind();
                txtListitems.Text = "";
                int selectedvalue = Convert.ToInt32(ddlFieldType.SelectedItem.Value);
                if (selectedvalue == 2 || selectedvalue == 3 || selectedvalue == 4)
                {
                    lblmsg.Style.Add("display", "block");
                    lblmsgCtrl.Style.Add("display", "block");
                    lblmsgCtrl.Text = ddlFieldType.SelectedItem.Text;
                    txtListitems.Style.Add("display", "block");
                    btnSaveControlListItems.Style.Add("display", "block");
                    TableListItem.Style.Add("display", "block");
                    panellist.Style.Add("display", "block");
                }
                //if (ddlCategory.SelectedItem.Text == "<< Add New >>")
                //{
                //    TableAddNew.Style.Add("display", "block");
                //}
                ModalPopupExtender1.Show();
            }
            catch { }
            finally
            {
                dt = null;
            }

        }

        protected void gvControlListItem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Delete")
                {
                    GridViewRow gvrow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                    int rowindex = Convert.ToInt32(gvrow.DataItemIndex);
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ListItem");
                    dt.Columns.Add("FieldType");
                    for (int i = 0; i < gvControlListItem.Rows.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        Label lb1 = (Label)gvControlListItem.Rows[i].FindControl("lblddllistitem");
                        dr["ListItem"] = lb1.Text;
                        dr["FieldType"] = ddlFieldType.SelectedItem.Text;
                        dt.Rows.Add(dr);
                    }
                    dt.Rows.RemoveAt(rowindex);
                    gvControlListItem.DataSource = dt;
                    gvControlListItem.DataBind();
                    ModalPopupExtender1.Show();
                }
            }
            catch { }
        }

        protected void gvControlListItem_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void deletelistitemgrid()
        {
            gvControlListItem.DataSource = null;
            gvControlListItem.DataBind();
        }

        private void PopulateExtraFieldId()
        {
            OpenSISWCFServiceStudentSetupClient stId = new OpenSISWCFServiceStudentSetupClient();
            try
            {
              int extrafield =  stId.getExtraFieldId();
              hdnExtraFldId.Value = extrafield.ToString();
            }
            catch
            {
            }
            finally
            {
                if (stId.State == CommunicationState.Opened)
                {
                    stId.Close();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceStudentSetupClient StudentSetupListItem = new OpenSISWCFServiceStudentSetupClient();
            OpenSISWCFServiceStudentSetupClient StudentSetupCTBL = new OpenSISWCFServiceStudentSetupClient();
            try
            {
                PopulateExtraFieldId();
                int ExtraFieldId;
                foreach (GridViewRow a in gvControlListItem.Rows)
                {
                    Label lbllistitem = (Label)a.FindControl("lblddllistitem");
                    string DdlListitemName = lbllistitem.Text.ToString().Trim();
                    ExtraFieldId = Convert.ToInt32(hdnExtraFldId.Value);
                    StudentSetupListItem.AddDdlListItem(ExtraFieldId.ToString(),DdlListitemName);
                }
                int SchoolId = Convert.ToInt32(Session["SchoolID"]);
                //if (ddlCategory.SelectedItem.Text == "<< Add New >>")//adding new category
                //{
                //    string CategoryName = txtCategoryName.Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;";
                //    StudentSetupCTBL.AddCategory(CategoryName,SchoolId);
                //}
                string FieldName = txtFieldName.Text.ToString().Trim();
                int CategoryId;
                //if (ddlCategory.SelectedItem.Text == "<< Add New >>")
                //{
                //    DataTable MaxCatId = new DataTable();
                //    MaxCatId = StudentSetupCTBL.GetMaxCatergoryId();
                //    CategoryId = Convert.ToInt32(MaxCatId.Rows[0]["MaxCatId"]);
                //}
                //else
                //{
                CategoryId = Convert.ToInt32(TreeView1.SelectedNode.Value.Split(",".ToCharArray())[0]);
                //}
                int ControlType = 0;
                if (ddlFieldType.SelectedItem.Text == "TextBox")
                {
                     ControlType = Convert.ToInt32("1");
                }
                if (ddlFieldType.SelectedItem.Text == "CheckBox")
                {
                     ControlType = Convert.ToInt32("2");
                }
                if (ddlFieldType.SelectedItem.Text == "RadioButton")
                {
                     ControlType = Convert.ToInt32("3");
                }
                if (ddlFieldType.SelectedItem.Text == "DropDownList")
                {
                     ControlType = Convert.ToInt32("4");
                }
                if (ddlFieldType.SelectedItem.Text == "Label")
                {
                     ControlType = Convert.ToInt32("5");
                }
                if (ddlFieldType.SelectedItem.Text == "DatePicker")
                {
                    ControlType = Convert.ToInt32("6");
                }               
               int StudentId = Convert.ToInt32("0");
               int StaffTypeId = Convert.ToInt32("0");
               ExtraFieldId = Convert.ToInt32(hdnExtraFldId.Value);

               if (TreeView1.SelectedNode.Value.Split(",".ToCharArray())[0] == "8")
               {
                   if (Session["SplEduFieldId"] != null)
                   {
                       if (Session["SplEduFieldId"].ToString() == "208")
                       {
                           StudentSetupCTBL.AddSpecialEducationControl(FieldName, Convert.ToString(ControlType), rbtnReq.SelectedItem.Value.ToString(), "208", "C", SchoolId);
                       }
                       else if (Session["SplEduFieldId"].ToString() == "209")
                       {
                           StudentSetupCTBL.AddGiftedStudentControl(FieldName, Convert.ToString(ControlType), rbtnReq.SelectedItem.Value.ToString(), "209", "C", SchoolId);
                       }
                   }
               }
               else
               {
                   StudentSetupCTBL.ControlAdd(Convert.ToString(ExtraFieldId), FieldName, Convert.ToString(ControlType), SchoolId, Convert.ToString(StudentId), Convert.ToString(StaffTypeId), Convert.ToString(CategoryId), rbtnReq.SelectedItem.Value.ToString());
               }
               deletelistitemgrid();                
                txtFieldName.Text = "";
                txtCategoryName.Text = "";
                ddlFieldType.SelectedIndex = 0;
                //lblmsg.Visible = false;
                lblmsg.Style.Add("display", "none");
                //lblmsgCtrl.Visible = false;
                lblmsgCtrl.Style.Add("display", "none");
                //txtListitems.Visible = false;
                txtListitems.Style.Add("display", "none");
                //btnSaveControlListItems.Visible = false;
                btnSaveControlListItems.Style.Add("display", "none");
                //populateStudentSetupCategory();
                //ddlCategory.SelectedIndex = 0;
                //TableAddNew.Visible = false;
                //TableAddNew.Style.Add("display", "none");
                TableListItem.Style.Add("display", "none");
                PopulateSchoolCategory();
                PopulateCategoryTree();
            }
            catch
            {
            }
            finally
            {  
                if (StudentSetupListItem.State == CommunicationState.Opened)
                {
                    StudentSetupListItem.Close();
                }
                if (StudentSetupCTBL.State == CommunicationState.Opened)
                {
                    StudentSetupCTBL.Close();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                deletelistitemgrid();
                txtFieldName.Text = "";
                txtCategoryName.Text = "";
                ddlFieldType.SelectedIndex = 0;
                //lblmsg.Visible = false;
                lblmsg.Style.Add("display", "none");
                //lblmsgCtrl.Visible = false;
                lblmsgCtrl.Style.Add("display", "none");
                //txtListitems.Visible = false;
                txtListitems.Style.Add("display", "none");
                //btnSaveControlListItems.Visible = false;
                btnSaveControlListItems.Style.Add("display", "none");
                //ddlCategory.SelectedIndex = 0;
                //TableAddNew.Visible = false;
                //TableAddNew.Style.Add("display", "none");
                TableListItem.Style.Add("display", "none");
                PopulateSchoolCategory();
                PopulateCategoryTree();
                ModalPopupExtender1.Enabled = false;
                ModalPopupExtender1.Hide();             
            }
            catch
            {
            }
        }        
        
        protected void gvCategoryFields_RowDataBound(object sender, GridViewRowEventArgs e)
        {            
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HiddenField hdnFieldType = (HiddenField)e.Row.FindControl("hdnFieldTypeCatFld");
                    Label lblFieldType = (Label)e.Row.FindControl("lblFieldTypeCatFld");
                    HiddenField hdnStatus = (HiddenField)e.Row.FindControl("hdnStatusCatFld");
                    //Image ibtnOn = (Image)e.Row.FindControl("ibtnOnCatFld");
                    GridView gvAddressFields = (GridView)e.Row.FindControl("gvAddressFields");
                    GridView gvSpecialEd = (GridView)e.Row.FindControl("gvSpecialEd");
                    HiddenField hdnDefaultOrCustom = (HiddenField)e.Row.FindControl("hdnDefaultOrCustomCatFld");
                    HiddenField hdnFieldId = (HiddenField)e.Row.FindControl("hdnFieldIdCatFld");
                    Label lblFieldDisplaytype = (Label)e.Row.FindControl("lblFieldDisplaytypeCatFld");
                    Label lblDisplForm = (Label)e.Row.FindControl("lblDisplForm");
                    //Image ibtnDataEntryCategoryFields = (Image)e.Row.FindControl("ibtnDataEntryCategoryFields");
                    //Image ibtnDataDisplayCategoryFields = (Image)e.Row.FindControl("ibtnDataDisplayCategoryFields");
                    HiddenField hdnFieldView = (HiddenField)e.Row.FindControl("hdnFieldViewCatFld");
                    HiddenField hdnIsMandatory = (HiddenField)e.Row.FindControl("hdnIsMandatoryCatFld");
                    //if (hdnFieldView.Value.ToString().Trim() == "V")
                    //{
                    //    ibtnDataDisplayCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataEntryCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "E")
                    //{
                    //    ibtnDataEntryCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "V_E")
                    //{
                    //    ibtnDataEntryCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "N" || hdnFieldView.Value.ToString().Trim() == "")
                    //{
                    //    ibtnDataEntryCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //    ibtnDataDisplayCategoryFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //if (hdnIsMandatory.Value.ToString() == "M")
                    //{
                    //    //ibtnDelete.Visible = false;
                    //    // ibtnOff.Attributes.Add("onclick", "return OpenModal('" + ModalPopupExtender3.ClientID + "');");
                    //}
                    //else
                    //{
                    //    //ibtnOff.Attributes.Add("onclick", " toolTip('')");
                    //}
                    int FieldId = Convert.ToInt32(hdnFieldId.Value);                    
                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
                    //if (hdnStatus.Value.ToString() == "A")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //    // ibtnOff.ImageUrl = "~/App_Themes/main/images/Default.png";
                    //}
                    //else if (hdnStatus.Value.ToString() == "I")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/Off.png";
                    //    // ibtnOn.ImageUrl = "~/App_Themes/main/images/Default.png";
                    //    ibtnDataEntryCategoryFields.Visible = false;
                    //    ibtnDataDisplayCategoryFields.Visible = false;
                    //}
                    //else
                    //{
                    //    //ibtnOff.ImageUrl = "~/App_Themes/main/images/Default.png";
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //}
                    if (hdnFieldType.Value == "1")
                    {
                        lblFieldType.Text = "TextBox";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "2")
                    {
                        lblFieldType.Text = "CheckBox";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "3")
                    {
                        lblFieldType.Text = "RadioButton";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "4")
                    {
                        lblFieldType.Text = "DropDownList";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "5")
                    {
                        lblFieldType.Text = "Label";
                        //lblFieldDisplaytype.Text = "Display Field";
                    }
                    else if (hdnFieldType.Value == "6")
                    {
                        lblFieldType.Text = "DatePicker";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    //ibtnOn.Attributes.Add("onmouseover", "toolTip('" + TTL.On + "')");
                    //ibtnOn.Attributes.Add("onmouseout", "toolTip()");
                    //ibtnOn.Attributes.Add("onclick", " toolTip('')");
                    //ibtnOff.Attributes.Add("onmouseover", "toolTip('" + TTL.Off + "')");
                    //ibtnOff.Attributes.Add("onmouseout", "toolTip()");
                    //ibtnOff.Attributes.Add("onclick", " toolTip('')");
                    OpenSISWCFServiceStudentSetupClient StSuBL = new OpenSISWCFServiceStudentSetupClient();
                    int addressfieldcount = StSuBL.CheckDefaultAddressFieldCount(FieldId);
                    StSuBL.Close();

                    if (addressfieldcount > 0)
                    {
                        OpenSISWCFServiceStudentSetupClient StSuBL2 = new OpenSISWCFServiceStudentSetupClient();
                        DataTable dt = StSuBL2.GetAddressFieldsByFieldId(SchoolId, FieldId, Session["MLUICulture"].ToString());
                        StSuBL2.Close();
                        gvAddressFields.DataSource = dt;
                        gvAddressFields.DataBind();
                        //DataTable dtadd = StSuBL.GetAdditionalContact();
                        //gvAddressFields.DataSource = dtadd;
                        //gvAddressFields.DataBind();
                        if (FieldId.ToString() == "152" || FieldId.ToString() == "153" || FieldId.ToString() == "154" || FieldId.ToString() == "155")
                        {
                            lblFieldDisplaytype.Text = "";
                        }

                    }
                    if (hdnFieldId.Value.ToString() == "208")
                    {
                        OpenSISWCFServiceStudentSetupClient StSuBL3 = new OpenSISWCFServiceStudentSetupClient();
                        DataTable dt = StSuBL3.GetSpecialEducation(SchoolId, Session["MLUICulture"].ToString());
                        StSuBL3.Close();
                        gvSpecialEd.DataSource = dt;
                        gvSpecialEd.DataBind();
                        lblFieldDisplaytype.Text = "";
                    }
                    if (hdnFieldId.Value.ToString() == "209")
                    {
                        OpenSISWCFServiceStudentSetupClient StSuBL4 = new OpenSISWCFServiceStudentSetupClient();
                        DataTable dt = StSuBL4.GetGiftedEducation(SchoolId, Session["MLUICulture"].ToString());
                        StSuBL4.Close();
                        gvSpecialEd.DataSource = dt;
                        gvSpecialEd.DataBind();
                        lblFieldDisplaytype.Text = "";
                    }
                    if (hdnDefaultOrCustom.Value.ToString() == "AD")
                    {
                        //int additionalcontactfieldcount = StSuBL.CheckAdditionalContactList();
                        //if (additionalcontactfieldcount > 0)
                        //{
                        OpenSISWCFServiceStudentSetupClient StSuBL5 = new OpenSISWCFServiceStudentSetupClient();
                        DataTable dt = StSuBL5.GetAdditionalContact(SchoolId, Session["MLUICulture"].ToString());
                        StSuBL5.Close();
                        dt.Columns.Add("Default_Custom");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            dt.Rows[i]["Default_Custom"] = "C";
                        }
                        //DataRow dr = null;
                        //dr = dt.NewRow() ;
                        //dr["Default_Custom"] = "C";
                        //dt.Rows.Add(dr);
                        gvAddressFields.DataSource = dt;
                        gvAddressFields.DataBind();
                        //}
                    }
                }
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    int catid = Convert.ToInt32(hdnCategoryId.Value);
                    if (catid == 3)
                    {
                        TableCell tc = e.Row.Cells[0];
                        tc.Text = "";
                        TableCell tc1 = e.Row.Cells[1];
                        tc1.Text = "";
                        TableCell tc2 = e.Row.Cells[2];
                        tc2.Text = "";
                    }
                }
            }
            catch { }
            finally
            {               
            }
        }

        protected void gvAddressFields_RowDataBound(object sender, GridViewRowEventArgs e)
        {            
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HiddenField hdnFieldType = (HiddenField)e.Row.FindControl("hdnFieldTypeAddFld");
                    Label lblFieldType = (Label)e.Row.FindControl("lblFieldTypeAddFld");
                    HiddenField hdnStatus = (HiddenField)e.Row.FindControl("hdnStatusAddFld");
                    //Image ibtnOn = (Image)e.Row.FindControl("ibtnOnAddFld");
                    //ImageButton ibtnOff = (ImageButton)e.Row.FindControl("ibtnOff");
                    Label lblFieldDisplaytype = (Label)e.Row.FindControl("lblFieldDisplaytypeAddFld");
                    HiddenField hdnFieldId = (HiddenField)e.Row.FindControl("hdnFieldIdAddFld");
                    HiddenField hdnFieldView = (HiddenField)e.Row.FindControl("hdnFieldViewAddFld");
                    //Image ibtnDataEntryAddressFields = (Image)e.Row.FindControl("ibtnDataEntryAddressFields");
                    //Image ibtnDataDisplayAddressFields = (Image)e.Row.FindControl("ibtnDataDisplayAddressFields");
                    HiddenField hdnIsMandatory = (HiddenField)e.Row.FindControl("hdnIsMandatoryAddFld");
                    //ImageButton ibtnDelete = (ImageButton)e.Row.FindControl("ibtnDelete");
                    //if (hdnFieldView.Value.ToString().Trim() == "V")
                    //{
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "E")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "V_E")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "N" || hdnFieldView.Value.ToString().Trim() == "")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    if (hdnIsMandatory.Value.ToString() == "M")
                    {
                        // ibtnDelete.Visible = false;
                        //ibtnOff.Attributes.Add("onclick", "return OpenModal('" + ModalPopupExtender3.ClientID + "');");
                    }
                    else
                    {
                        // ibtnOff.Attributes.Add("onclick", " toolTip('')");
                    }
                    int FieldId = Convert.ToInt32(hdnFieldId.Value);                    
                    //if (hdnStatus.Value.ToString() == "A")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //}
                    //else if (hdnStatus.Value.ToString() == "I")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/Off.png";
                    //    ibtnDataEntryAddressFields.Visible = false;
                    //    ibtnDataDisplayAddressFields.Visible = false;
                    //}
                    //else
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //}
                    if (hdnFieldType.Value == "1")
                    {
                        lblFieldType.Text = "TextBox";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "2")
                    {
                        lblFieldType.Text = "CheckBox";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "3")
                    {
                        lblFieldType.Text = "RadioButton";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "4")
                    {
                        lblFieldType.Text = "DropDownList";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                    else if (hdnFieldType.Value == "5")
                    {
                        lblFieldType.Text = "Label";
                        //lblFieldDisplaytype.Text = "Display Field";
                    }
                    else if (hdnFieldType.Value == "6")
                    {
                        lblFieldType.Text = "DatePicker";
                        //lblFieldDisplaytype.Text = "Entry Field";
                    }
                }
            }
            catch { }
            finally
            {
                //StSuBL = null;
            }
        }

        protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
        {
            int FieldId;
            int SchoolId;
            try
            {
                foreach (GridViewRow gvrow in gvCategoryFields.Rows)
                {
                    CheckBox chkrow = (CheckBox)gvrow.FindControl("chkrowCatFld");
                    if (chkrow.Checked)
                    {
                        hdnAddressfield.Value = "NA";
                        HiddenField hdnFieldView = (HiddenField)gvrow.FindControl("hdnFieldViewCatFld");
                        HiddenField hdnStatus = (HiddenField)gvrow.FindControl("hdnStatusCatFld");
                        HiddenField hdnIsMandatory = (HiddenField)gvrow.FindControl("hdnIsMandatoryCatFld");
                        HiddenField hdnFieldId = (HiddenField)gvrow.FindControl("hdnFieldIdCatFld");
                        Label lblFieldName = (Label)gvrow.FindControl("lblFieldNameCatFld");
                        HiddenField hdnDefaultOrCustom = (HiddenField)gvrow.FindControl("hdnDefaultOrCustomCatFld");
                        hdnFldid.Value = hdnFieldId.Value.ToString();
                        hdnDefaultCustom.Value = hdnDefaultOrCustom.Value.ToString();
                        hdnMandatory.Value = hdnIsMandatory.Value.ToString();
                        txtFieldNameEdit.Text = lblFieldName.Text;
                        //if (hdnFieldView.Value.ToString().Trim() == "V")
                        //{
                        //    chkDatadisplay.Checked = true;
                        //    chkDataEntry.Checked = false;
                        //}
                        //else if (hdnFieldView.Value.ToString().Trim() == "E")
                        //{
                        //    chkDatadisplay.Checked = false;
                        //    chkDataEntry.Checked = true;
                        //}
                        //else if (hdnFieldView.Value.ToString().Trim() == "V_E")
                        //{
                        //    chkDatadisplay.Checked = true;
                        //    chkDataEntry.Checked = true;
                        //}
                        //else if (hdnFieldView.Value.ToString().Trim() == "N" || hdnFieldView.Value.ToString().Trim() == "")
                        //{
                        //    chkDatadisplay.Checked = false;
                        //    chkDataEntry.Checked = false;
                        //}
                        if (hdnIsMandatory.Value.ToString() == "M")
                        {
                            rdntRequired.Items[0].Selected = true;
                            rdntRequired.Items[1].Selected = false;
                        }
                        else
                        {
                            rdntRequired.Items[0].Selected = false;
                            rdntRequired.Items[1].Selected = true;
                        }                       
                        FieldId = Convert.ToInt32(hdnFieldId.Value);
                        SchoolId = Convert.ToInt32(Session["SchoolID"]);
                        //if (hdnStatus.Value.ToString() == "A")
                        //{
                        //    rdnOnOff.Items[0].Selected = true;
                        //    rdnOnOff.Items[1].Selected = false;
                        //}
                        //else if (hdnStatus.Value.ToString() == "I")
                        //{
                        //    rdnOnOff.Items[1].Selected = true;
                        //    rdnOnOff.Items[0].Selected = false;
                        //    chkDatadisplay.Enabled = false;
                        //    chkDataEntry.Enabled = false;
                        //}
                        //else
                        //{
                        //    rdnOnOff.Items[0].Selected = true;
                        //}
                    }
                    GridView gvAddressFields = (GridView)gvrow.FindControl("gvAddressFields");
                    foreach (GridViewRow gvrowadd in gvAddressFields.Rows)
                    {
                        CheckBox chk = (CheckBox)gvrowadd.FindControl("chkrowAddFld");
                        if (chk.Checked)
                        {
                            hdnAddressfield.Value = "A";
                            HiddenField hdnFieldViewadd = (HiddenField)gvrowadd.FindControl("hdnFieldViewAddFld");
                            HiddenField hdnStatusadd = (HiddenField)gvrowadd.FindControl("hdnStatusAddFld");
                            HiddenField hdnIsMandatoryadd = (HiddenField)gvrowadd.FindControl("hdnIsMandatoryAddFld");
                            HiddenField hdnFieldIdadd = (HiddenField)gvrowadd.FindControl("hdnFieldIdAddFld");
                            HiddenField hdnDefaultOrCustomadd = (HiddenField)gvrowadd.FindControl("hdnDefaultOrCustomAddFld");
                            Label lblFieldName = (Label)gvrowadd.FindControl("lblFieldNameAddFld");
                            hdnDefaultCustom.Value = hdnDefaultOrCustomadd.Value.ToString();
                            hdnFldid.Value = hdnFieldIdadd.Value.ToString();
                            hdnMandatory.Value = hdnIsMandatoryadd.Value.ToString();
                            txtFieldNameEdit.Text = lblFieldName.Text;
                            //if (hdnFieldViewadd.Value.ToString().Trim() == "V")
                            //{
                            //    chkDatadisplay.Checked = true;
                            //    chkDataEntry.Checked = false;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "E")
                            //{
                            //    chkDatadisplay.Checked = false;
                            //    chkDataEntry.Checked = true;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "V_E")
                            //{
                            //    chkDatadisplay.Checked = true;
                            //    chkDataEntry.Checked = true;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "N" || hdnFieldViewadd.Value.ToString().Trim() == "")
                            //{
                            //    chkDatadisplay.Checked = false;
                            //    chkDataEntry.Checked = false;
                            //}
                            if (hdnIsMandatoryadd.Value.ToString() == "M")
                            {
                                rdntRequired.Items[0].Selected = true;
                                rdntRequired.Items[1].Selected = false;
                            }
                            else
                            {
                                rdntRequired.Items[0].Selected = false;
                                rdntRequired.Items[1].Selected = true;
                            }                           
                            FieldId = Convert.ToInt32(hdnFieldIdadd.Value);
                            SchoolId = Convert.ToInt32(Session["SchoolID"]);
                            //if (hdnStatusadd.Value.ToString() == "A")
                            //{
                            //    rdnOnOff.Items[0].Selected = true;
                            //    rdnOnOff.Items[1].Selected = false;
                            //}
                            //else if (hdnStatusadd.Value.ToString() == "I")
                            //{
                            //    rdnOnOff.Items[1].Selected = true;
                            //    rdnOnOff.Items[0].Selected = false;
                            //    chkDatadisplay.Enabled = false;
                            //    chkDataEntry.Enabled = false;
                            //}
                            //else
                            //{
                            //    rdnOnOff.Items.FindByValue("A").Selected = true;
                            //}
                        }
                    }
                    GridView gvSpecialEd = (GridView)gvrow.FindControl("gvSpecialEd");
                    foreach (GridViewRow gvrowspl in gvSpecialEd.Rows)
                    {
                        CheckBox chk = (CheckBox)gvrowspl.FindControl("chkRowSplId");
                        if (chk.Checked)
                        {
                            HiddenField hdnFieldViewadd = (HiddenField)gvrowspl.FindControl("hdnFieldViewSplId");
                            HiddenField hdnStatusadd = (HiddenField)gvrowspl.FindControl("hdnStatusSplId");
                            HiddenField hdnIsMandatoryadd = (HiddenField)gvrowspl.FindControl("hdnIsMandatorySplId");
                            HiddenField hdnFieldIdadd = (HiddenField)gvrowspl.FindControl("hdnFieldIdSplId");
                            HiddenField hdnDefaultOrCustomadd = (HiddenField)gvrowspl.FindControl("hdnDefaultOrCustomSplId");
                            Label lblFieldName = (Label)gvrowspl.FindControl("lblFieldNameSplId");
                            HiddenField hdnDeffldid = (HiddenField)gvrowspl.FindControl("hdnDeffldidSplId");
                            if (hdnDeffldid.Value.ToString() == "208")
                            {
                                hdnAddressfield.Value = "SPED";
                            }
                            if (hdnDeffldid.Value.ToString() == "209")
                            {
                                hdnAddressfield.Value = "GIFED";
                            }
                            //hdnDefaultCustom.Value = hdnDefaultOrCustomadd.Value.ToString();
                            hdnFldid.Value = hdnFieldIdadd.Value.ToString();
                            hdnMandatory.Value = hdnIsMandatoryadd.Value.ToString();
                            txtFieldNameEdit.Text = lblFieldName.Text;
                            //if (hdnFieldViewadd.Value.ToString().Trim() == "V")
                            //{
                            //    chkDatadisplay.Checked = true;
                            //    chkDataEntry.Checked = false;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "E")
                            //{
                            //    chkDatadisplay.Checked = false;
                            //    chkDataEntry.Checked = true;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "V_E")
                            //{
                            //    chkDatadisplay.Checked = true;
                            //    chkDataEntry.Checked = true;
                            //}
                            //else if (hdnFieldViewadd.Value.ToString().Trim() == "N" || hdnFieldViewadd.Value.ToString().Trim() == "")
                            //{
                            //    chkDatadisplay.Checked = false;
                            //    chkDataEntry.Checked = false;
                            //}
                            if (hdnIsMandatoryadd.Value.ToString().Trim() == "M")
                            {
                                rdntRequired.Items[0].Selected = true;
                                rdntRequired.Items[1].Selected = false;
                            }
                            else
                            {
                                rdntRequired.Items[0].Selected = false;
                                rdntRequired.Items[1].Selected = true;
                            }
                            FieldId = Convert.ToInt32(hdnFieldIdadd.Value);
                            SchoolId = Convert.ToInt32(Session["SchoolID"]);
                            //if (hdnStatusadd.Value.ToString() == "A")
                            //{
                            //    rdnOnOff.Items[0].Selected = true;
                            //    rdnOnOff.Items[1].Selected = false;
                            //}
                            //else if (hdnStatusadd.Value.ToString() == "I")
                            //{
                            //    rdnOnOff.Items[1].Selected = true;
                            //    rdnOnOff.Items[0].Selected = false;
                            //    chkDatadisplay.Enabled = false;
                            //    chkDataEntry.Enabled = false;
                            //}
                            //else
                            //{
                            //    rdnOnOff.Items.FindByValue("A").Selected = true;
                            //}
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
               // StSuBL = null;
            }
            ModalPopupExtender2.Enabled = true;
            ModalPopupExtender2.Show();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
            int SchoolId = 0;
            int FieldId = 0;
            string IsMandatory = rdntRequired.SelectedItem.Value;

            if (hdnAddressfield.Value == "NA")
            {
                string DefaultOrCustom = hdnDefaultCustom.Value.ToString();
                SchoolId = Convert.ToInt32(Session["SchoolID"]);
                string fieldid = hdnFldid.Value;
                FieldId = Convert.ToInt32(fieldid);
                if (DefaultOrCustom == "D")
                {
                    FieldId = Convert.ToInt32(fieldid);
                    schoolcategory.UpdateDefaultFieldRequired(FieldId, txtFieldNameEdit.Text, IsMandatory);
                    PopulateSchoolCategory();
                }
                else if (DefaultOrCustom == "C")
                {
                    FieldId = Convert.ToInt32(fieldid);
                    schoolcategory.UpdateCustomFieldRequired(FieldId, txtFieldNameEdit.Text, IsMandatory);
                    PopulateSchoolCategory();
                }
            }
            else if (hdnAddressfield.Value == "A")
            {
                string DefaultOrCustom = hdnDefaultCustom.Value.ToString();               
                SchoolId = Convert.ToInt32(Session["SchoolID"]);
                string fieldid = hdnFldid.Value;
                FieldId = Convert.ToInt32(fieldid);
                if (DefaultOrCustom == "D")
                {
                    FieldId = Convert.ToInt32(fieldid);
                    schoolcategory.UpdateDefaultAddressFieldRequired(FieldId, txtFieldNameEdit.Text, IsMandatory);
                    PopulateSchoolCategory();   
                }
                else if (DefaultOrCustom == "C")
                {
                    FieldId = Convert.ToInt32(fieldid);
                    schoolcategory.UpdateAdditionalAddressFieldRequired(FieldId,txtFieldNameEdit.Text,IsMandatory);
                    PopulateSchoolCategory();
                }
            }
            else if (hdnAddressfield.Value == "SPED")
            {
                SchoolId = Convert.ToInt32(Session["SchoolID"]);
                string fieldid = hdnFldid.Value;
                FieldId = Convert.ToInt32(fieldid);
                schoolcategory.UpdateDefaultSpecialEducationFieldRequired(FieldId,txtFieldNameEdit.Text,IsMandatory);
                PopulateSchoolCategory();               
            }
            else if (hdnAddressfield.Value == "GIFED")
            {
                SchoolId = Convert.ToInt32(Session["SchoolID"]);
                string fieldid = hdnFldid.Value;
                FieldId = Convert.ToInt32(fieldid);
                schoolcategory.UpdateDefaultGiftedFieldRequired(FieldId,txtFieldNameEdit.Text,IsMandatory);
                PopulateSchoolCategory();
            }
            if (schoolcategory.State == CommunicationState.Opened)
            {
                schoolcategory.Close();
            }
        }

        protected void btnCancelEdit_Click(object sender, EventArgs e)
        {
            txtFieldNameEdit.Text = "";
            //rdnOnOff.SelectedIndex = -1;
            //chkDataEntry.Checked = false;
            //chkDatadisplay.Checked = false;
            //chkDataEntry.Enabled = true;
            //chkDatadisplay.Enabled = true;
            PopulateSchoolCategory();
            ModalPopupExtender2.Hide();
        }      

        protected void btnok_Click(object sender, EventArgs e)
        {
            if (hdnmsg.Value.ToString() != "UC")
            {
                ModalPopupExtender2.Show();
            }
            else
            {
                ModalPopupExtender3.Hide();
            }
        }

        protected void rdnOnOff_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (rdnOnOff.SelectedValue == "A")
            //{
            //    chkDataEntry.Enabled = true;
            //    chkDatadisplay.Enabled = true;
            //    ModalPopupExtender2.Show();
            //}
            //if (rdnOnOff.SelectedValue == "I")
            //{
            //    chkDataEntry.Enabled = false;
            //    chkDatadisplay.Enabled = false;
            //    ModalPopupExtender2.Show();
            //}
        }

        protected void imgbtnDelete_Click(object sender, ImageClickEventArgs e)
        {
            OpenSISWCFServiceStudentSetupClient StudentSetupCTBL = new OpenSISWCFServiceStudentSetupClient();
            string AlertMessage = "";
           
            try
            {
                foreach (GridViewRow gvrow in gvCategoryFields.Rows)
                {
                    CheckBox chkrow = (CheckBox)gvrow.FindControl("chkrowCatFld");
                    if (chkrow.Checked)
                    {
                        HiddenField hdnFieldId = (HiddenField)gvrow.FindControl("hdnFieldIdCatFld");
                        HiddenField hdnDefaultOrCustom = (HiddenField)gvrow.FindControl("hdnDefaultOrCustomCatFld");
                        if (hdnDefaultOrCustom.Value.Trim().ToString() == "D")
                        {
                            AlertMessage = "System Default Field Can Not Delete";
                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + AlertMessage + "');", true);
                            break;
                        }
                        else
                        {
                           AlertMessage= StudentSetupCTBL.DeleteStudentCustomField(Convert.ToInt32(hdnFieldId.Value));
                            if (AlertMessage != "")
                            {
                                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + AlertMessage + "');", true);
                            }
                        }
                    }
                }
                PopulateSchoolCategory();
            }
            catch
            {
            }
            finally
            {
                StudentSetupCTBL = null;
            }
        }
       
        //Select All options
        //protected void ibtnSelectallEntry_Click(object sender, ImageClickEventArgs e)
        //{
        //   // StudentSetupBL schoolcategory = new StudentSetupBL();
        //    OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
        //    foreach (GridViewRow gvr in gvCategoryFields.Rows)
        //    {
        //        CheckBox chkrow = (CheckBox)gvr.FindControl("chkrowCatFld");
        //        if (chkrow.Checked)
        //        {
        //            HiddenField hdnFieldType = (HiddenField)gvr.FindControl("hdnFieldTypeCatFld");
        //            Label lblFieldType = (Label)gvr.FindControl("lblFieldTypeCatFld");
        //            HiddenField hdnStatus = (HiddenField)gvr.FindControl("hdnStatusCatFld");
        //            HiddenField hdnDefaultOrCustom = (HiddenField)gvr.FindControl("hdnDefaultOrCustomCatFld");
        //            HiddenField hdnFieldId = (HiddenField)gvr.FindControl("hdnFieldIdCatFld");
        //            Label lblFieldDisplaytype = (Label)gvr.FindControl("lblFieldDisplaytypeCatFld");
        //            Label lblDisplForm = (Label)gvr.FindControl("lblDisplFormCatFld");
        //            HiddenField hdnFieldViews = (HiddenField)gvr.FindControl("hdnFieldViewCatFld");
        //            HiddenField hdnIsMandatory = (HiddenField)gvr.FindControl("hdnIsMandatoryCatFld");
        //            string DefaultOrCustom = hdnDefaultOrCustom.Value.ToString();
        //            string IsMandatory = hdnIsMandatory.Value.ToString();
        //            string fieldview;
        //            int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //            string fieldid = hdnFieldId.Value;
        //            string fieldviews = hdnFieldViews.Value;
        //            string FieldView = "";
        //            int FieldId = Convert.ToInt32(fieldid);
        //            string FieldStatus = "";
        //            if (DefaultOrCustom == "D")
        //            {
        //                if (schoolcategory.CheckDefaultFieldCount(FieldId,SchoolId) > 0)
        //                {
        //                    FieldId = Convert.ToInt32(fieldid);

        //                    if (fieldviews.Trim() == "V")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "E")
        //                    {
        //                        FieldView = "E";
        //                    }
        //                    if (fieldviews.Trim() == "V_E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                    {
        //                        FieldView = "E";
        //                    }
        //                    //schoolcategory.UpdateDefaultFieldStatus();
        //                    schoolcategory.UpdateDefaultFieldView(FieldView,FieldId,SchoolId);
        //                    PopulateSchoolCategory();
        //                }
        //                else
        //                {
        //                    FieldId = Convert.ToInt32(fieldid);
        //                    FieldStatus = "A";
        //                    if (fieldviews.Trim() == "V")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "E")
        //                    {
        //                        FieldView = "E";
        //                    }
        //                    if (fieldviews.Trim() == "V_E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "N" || fieldviews == "")
        //                    {
        //                        FieldView = "E";
        //                    }
        //                    if (hdnStatus.Value.Trim() == "I" || hdnStatus.Value.Trim() == "")
        //                    {
        //                        schoolcategory.InsertDefaultFieldStatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                    }
        //                    PopulateSchoolCategory();
        //                }
        //            }
        //            else if (DefaultOrCustom == "C")
        //            {
        //                FieldId = Convert.ToInt32(fieldid);
        //                FieldStatus = hdnStatus.Value.Trim();
        //                if (fieldviews.Trim() == "V")
        //                {
        //                    FieldView = "V_E";
        //                }
        //                if (fieldviews.Trim() == "E")
        //                {
        //                    FieldView = "E";
        //                }
        //                if (fieldviews.Trim() == "V_E")
        //                {
        //                    FieldView = "V_E";
        //                }
        //                if (fieldviews.Trim() == "N" || fieldviews == "")
        //                {
        //                    FieldView = "E";
        //                }
        //                schoolcategory.UpdateCustomFieldStatus(FieldStatus,Convert.ToString( FieldId));
        //                schoolcategory.UpdateCustomFieldView(FieldView,FieldId);
        //                PopulateSchoolCategory();
        //            }
        //        }
        //        GridView gvAddressFields = (GridView)gvr.FindControl("gvAddressFields");
        //        foreach (GridViewRow gvrowadd in gvAddressFields.Rows)
        //        {
        //            CheckBox chk = (CheckBox)gvrowadd.FindControl("chkRowAddFld");
        //            if (chk.Checked)
        //            {
        //                {
        //                    HiddenField hdnFieldTypeadd = (HiddenField)gvrowadd.FindControl("hdnFieldTypeAddFld");
        //                    Label lblFieldTypeadd = (Label)gvrowadd.FindControl("lblFieldTypeAddFld");
        //                    HiddenField hdnStatusadd = (HiddenField)gvrowadd.FindControl("hdnStatusAddFld");
        //                    Label lblFieldDisplaytypeadd = (Label)gvrowadd.FindControl("lblFieldDisplaytypeAddFld");
        //                    HiddenField hdnFieldIdadd = (HiddenField)gvrowadd.FindControl("hdnFieldIdAddFld");
        //                    HiddenField hdnFieldViewadd = (HiddenField)gvrowadd.FindControl("hdnFieldViewAddFld");
        //                    HiddenField hdnIsMandatoryadd = (HiddenField)gvrowadd.FindControl("hdnIsMandatoryAddFld");
        //                    HiddenField hdnDefaultOrCustomadd = (HiddenField)gvrowadd.FindControl("hdnDefaultOrCustomAddFld");
        //                    string DefaultOrCustom = hdnDefaultOrCustomadd.Value.ToString();
        //                    string IsMandatory = hdnIsMandatoryadd.Value.ToString();
        //                    string fieldview;
        //                    string fieldviews = hdnFieldViewadd.Value.ToString();
        //                    //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
        //                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //                    string fieldidadd = hdnFieldIdadd.Value;
        //                    string FieldStatus = "";
        //                    int FieldId = Convert.ToInt32(fieldidadd);
        //                    string FieldView = "";
        //                    if (DefaultOrCustom == "D")
        //                    {
        //                        if (schoolcategory.CheckAddressFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            schoolcategory.UpdateDefaultAddressFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultAddressFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.InsertDefaultAddressFieldStatus(FieldStatus, FieldId, SchoolId, FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                    else if (DefaultOrCustom == "C")
        //                    {
        //                        if (schoolcategory.CheckAdditionalAddressFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            schoolcategory.UpdateAdditionalAddressFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateAdditionalAddressFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.InsertAdditionalAddressFieldStatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        //gvspecial 
        //        GridView gvSpecialEd = (GridView)gvr.FindControl("gvSpecialEd");
        //        foreach (GridViewRow gvrowspeed in gvSpecialEd.Rows)
        //        {
        //            CheckBox chk = (CheckBox)gvrowspeed.FindControl("chkRowSplId");
        //            if (chk.Checked)
        //            {
        //                {
        //                    HiddenField hdnFieldTypeadd = (HiddenField)gvrowspeed.FindControl("hdnFieldTypeSplId");
        //                    Label lblFieldTypeadd = (Label)gvrowspeed.FindControl("lblFieldTypeSplId");
        //                    HiddenField hdnStatusadd = (HiddenField)gvrowspeed.FindControl("hdnStatusSplId");
        //                    Label lblFieldDisplaytypeadd = (Label)gvrowspeed.FindControl("lblFieldDisplaytypeSplId");
        //                    HiddenField hdnFieldIdadd = (HiddenField)gvrowspeed.FindControl("hdnFieldIdSplId");
        //                    HiddenField hdnFieldViewadd = (HiddenField)gvrowspeed.FindControl("hdnFieldViewSplId");
        //                    HiddenField hdnIsMandatoryadd = (HiddenField)gvrowspeed.FindControl("hdnIsMandatorySplId");
        //                    HiddenField hdnDeffldid = (HiddenField)gvrowspeed.FindControl("hdnDeffldidSplId");
        //                    //string DefaultOrCustom = hdnDefaultOrCustomadd.Value.ToString();
        //                    string IsMandatory = hdnIsMandatoryadd.Value.ToString();
        //                    string fieldview;
        //                    string deffieldid = hdnDeffldid.Value.ToString();
        //                    string fieldviews = hdnFieldViewadd.Value.ToString();
        //                    //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
        //                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //                    string fieldidadd = hdnFieldIdadd.Value;
        //                    int FieldId = Convert.ToInt32(fieldidadd);
        //                    string FieldStatus = "";
        //                    string FieldView = "";
        //                    if (deffieldid == "208")
        //                    {
        //                        if (schoolcategory.CheckSpecialFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            schoolcategory.UpdateDefaultSpecialFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultSpecialFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.Insertspecialfieldstatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                    else if (deffieldid == "209")
        //                    {
        //                        if (schoolcategory.CheckGiftedFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            schoolcategory.UpdateDefaultgiftedFieldViewStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultGiftedFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews == "")
        //                            {
        //                                FieldView = "E";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.Insertgiftedfieldstatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    if (schoolcategory.State == CommunicationState.Opened)
        //    {
        //        schoolcategory.Close();
        //    }
        //}

        //protected void ibtnSelectallView_Click(object sender, ImageClickEventArgs e)
        //{
        //   // StudentSetupBL schoolcategory = new StudentSetupBL();
        //    OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
        //    foreach (GridViewRow gvr in gvCategoryFields.Rows)
        //    {
        //        CheckBox chkrow = (CheckBox)gvr.FindControl("chkrowCatFld");
        //        if (chkrow.Checked)
        //        {
        //            HiddenField hdnFieldType = (HiddenField)gvr.FindControl("hdnFieldTypeCatFld");
        //            Label lblFieldType = (Label)gvr.FindControl("lblFieldTypeCatFld");
        //            HiddenField hdnStatus = (HiddenField)gvr.FindControl("hdnStatusCatFld");
        //            HiddenField hdnDefaultOrCustom = (HiddenField)gvr.FindControl("hdnDefaultOrCustomCatFld");
        //            HiddenField hdnFieldId = (HiddenField)gvr.FindControl("hdnFieldIdCatFld");
        //            Label lblFieldDisplaytype = (Label)gvr.FindControl("lblFieldDisplaytypeCatFld");
        //            Label lblDisplForm = (Label)gvr.FindControl("lblDisplFormCatFld");
        //            HiddenField hdnFieldViews = (HiddenField)gvr.FindControl("hdnFieldViewCatFld");
        //            HiddenField hdnIsMandatory = (HiddenField)gvr.FindControl("hdnIsMandatoryCatFld");
        //            string DefaultOrCustom = hdnDefaultOrCustom.Value.ToString();
        //            string IsMandatory = hdnIsMandatory.Value.ToString();
        //            string fieldview;
        //            //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
        //            int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //            string fieldid = hdnFieldId.Value;
        //            string fieldviews = hdnFieldViews.Value;
        //            int FieldId = Convert.ToInt32(fieldid);
        //            string FieldView = "";
        //            string FieldStatus = "";
        //            if (DefaultOrCustom == "D")
        //            {
        //                if (schoolcategory.CheckDefaultFieldCount(FieldId,SchoolId) > 0)
        //                {
        //                    FieldId = Convert.ToInt32(fieldid);
        //                    if (fieldviews.Trim() == "V")
        //                    {
        //                        FieldView = "V";
        //                    }
        //                    if (fieldviews.Trim() == "E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "V_E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                    {
        //                        FieldView = "V";
        //                    }
        //                    //schoolcategory.UpdateDefaultFieldStatus();
        //                    schoolcategory.UpdateDefaultFieldView(FieldView,FieldId,SchoolId);
        //                    PopulateSchoolCategory();
        //                }
        //                else
        //                {
        //                    FieldId = Convert.ToInt32(fieldid);
        //                    FieldStatus = "A";
        //                    if (fieldviews.Trim() == "V")
        //                    {
        //                        FieldView = "V";
        //                    }
        //                    if (fieldviews.Trim() == "E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "V_E")
        //                    {
        //                        FieldView = "V_E";
        //                    }
        //                    if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                    {
        //                        FieldView = "V";
        //                    }
        //                    if (hdnStatus.Value.Trim() == "I" || hdnStatus.Value.Trim() == "")
        //                    {
        //                        schoolcategory.InsertDefaultFieldStatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                    }
        //                    PopulateSchoolCategory();
        //                }
        //            }
        //            else if (DefaultOrCustom == "C")
        //            {
        //                FieldId = Convert.ToInt32(fieldid);
        //                FieldStatus = hdnStatus.Value.Trim();
        //                if (fieldviews.Trim() == "V")
        //                {
        //                    FieldView = "V";
        //                }
        //                if (fieldviews.Trim() == "E")
        //                {
        //                    FieldView = "V_E";
        //                }
        //                if (fieldviews.Trim() == "V_E")
        //                {
        //                    FieldView = "V_E";
        //                }
        //                if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                {
        //                    FieldView = "V";
        //                }
        //                schoolcategory.UpdateCustomFieldStatus(FieldStatus,Convert.ToString(FieldId));
        //                schoolcategory.UpdateCustomFieldView(FieldView,FieldId);
        //                PopulateSchoolCategory();
        //            }
        //        }
        //        GridView gvAddressFields = (GridView)gvr.FindControl("gvAddressFields");
        //        foreach (GridViewRow gvrowadd in gvAddressFields.Rows)
        //        {
        //            CheckBox chk = (CheckBox)gvrowadd.FindControl("chkRowAddFld");
        //            if (chk.Checked)
        //            {
        //                {
        //                    HiddenField hdnFieldTypeadd = (HiddenField)gvrowadd.FindControl("hdnFieldTypeAddFld");
        //                    Label lblFieldTypeadd = (Label)gvrowadd.FindControl("lblFieldTypeAddFld");
        //                    HiddenField hdnStatusadd = (HiddenField)gvrowadd.FindControl("hdnStatusAddFld");
        //                    Label lblFieldDisplaytypeadd = (Label)gvrowadd.FindControl("lblFieldDisplaytypeAddFld");
        //                    HiddenField hdnFieldIdadd = (HiddenField)gvrowadd.FindControl("hdnFieldIdAddFld");
        //                    HiddenField hdnFieldViewadd = (HiddenField)gvrowadd.FindControl("hdnFieldViewAddFld");
        //                    HiddenField hdnIsMandatoryadd = (HiddenField)gvrowadd.FindControl("hdnIsMandatoryAddFld");
        //                    HiddenField hdnDefaultOrCustomadd = (HiddenField)gvrowadd.FindControl("hdnDefaultOrCustomAddFld");
        //                    string DefaultOrCustom = hdnDefaultOrCustomadd.Value.ToString();
        //                    string IsMandatory = hdnIsMandatoryadd.Value.ToString();
        //                    string fieldview;
        //                    string fieldviews = hdnFieldViewadd.Value.ToString();
        //                    //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
        //                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //                    string fieldidadd = hdnFieldIdadd.Value;
        //                    int FieldId = Convert.ToInt32(fieldidadd);
        //                    string FieldStatus = "";
        //                    string FieldView = "";
        //                    if (DefaultOrCustom == "D")
        //                    {
        //                        if (schoolcategory.CheckAddressFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            schoolcategory.UpdateDefaultAddressFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultAddressFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.InsertDefaultAddressFieldStatus(FieldStatus, FieldId, SchoolId, FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                    else if (DefaultOrCustom == "C")
        //                    {
        //                        if (schoolcategory.CheckAdditionalAddressFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            schoolcategory.UpdateAdditionalAddressFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateAdditionalAddressFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.InsertAdditionalAddressFieldStatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        //gvspecial and gifted
        //        GridView gvSpecialEd = (GridView)gvr.FindControl("gvSpecialEd");
        //        foreach (GridViewRow gvrowspeed in gvSpecialEd.Rows)
        //        {
        //            CheckBox chk = (CheckBox)gvrowspeed.FindControl("chkRowSplId");
        //            if (chk.Checked)
        //            {
        //                {
        //                    HiddenField hdnFieldTypeadd = (HiddenField)gvrowspeed.FindControl("hdnFieldTypeSplId");
        //                    Label lblFieldTypeadd = (Label)gvrowspeed.FindControl("lblFieldTypeSplId");
        //                    HiddenField hdnStatusadd = (HiddenField)gvrowspeed.FindControl("hdnStatusSplId");
        //                    Label lblFieldDisplaytypeadd = (Label)gvrowspeed.FindControl("lblFieldDisplaytypeSplId");
        //                    HiddenField hdnFieldIdadd = (HiddenField)gvrowspeed.FindControl("hdnFieldIdSplId");
        //                    HiddenField hdnFieldViewadd = (HiddenField)gvrowspeed.FindControl("hdnFieldViewSplId");
        //                    HiddenField hdnIsMandatoryadd = (HiddenField)gvrowspeed.FindControl("hdnIsMandatorySplId");
        //                    HiddenField hdnDeffldid = (HiddenField)gvrowspeed.FindControl("hdnDeffldidSplId");
        //                    //string DefaultOrCustom = hdnDefaultOrCustomadd.Value.ToString();
        //                    string IsMandatory = hdnIsMandatoryadd.Value.ToString();
        //                    string fieldview;
        //                    string deffieldid = hdnDeffldid.Value.ToString();
        //                    string fieldviews = hdnFieldViewadd.Value.ToString();
        //                    //ddlSchoolName = (DropDownList)Page.Master.FindControl("ddlSchoolName");
        //                    int SchoolId = Convert.ToInt32(Session["SchoolID"]);
        //                    string fieldidadd = hdnFieldIdadd.Value;
        //                    int FieldId = Convert.ToInt32(fieldidadd);
        //                    string FieldStatus = "";
        //                    string FieldView = "";
        //                    if (deffieldid == "208")
        //                    {
        //                        if (schoolcategory.CheckSpecialFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            schoolcategory.UpdateDefaultSpecialFieldStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultSpecialFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (hdnStatusadd.Value.Trim() == "I" || hdnStatusadd.Value.Trim() == "")
        //                            {
        //                                schoolcategory.Insertspecialfieldstatus(FieldId,FieldStatus,SchoolId,FieldView);
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                    else if (deffieldid == "209")
        //                    {
        //                        if (schoolcategory.CheckGiftedFieldFromStatusTable(FieldId,SchoolId) > 0)
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = hdnStatusadd.Value.ToString();
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            schoolcategory.UpdateDefaultgiftedFieldViewStatus(FieldStatus,FieldId,SchoolId);
        //                            schoolcategory.UpdateDefaultGiftedFieldView(FieldView,FieldId,SchoolId);
        //                            PopulateSchoolCategory();
        //                        }
        //                        else
        //                        {
        //                            FieldId = Convert.ToInt32(fieldidadd);
        //                            FieldStatus = "A";
        //                            if (fieldviews.Trim() == "V")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            if (fieldviews.Trim() == "E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "V_E")
        //                            {
        //                                FieldView = "V_E";
        //                            }
        //                            if (fieldviews.Trim() == "N" || fieldviews.Trim() == "")
        //                            {
        //                                FieldView = "V";
        //                            }
        //                            PopulateSchoolCategory();
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    if (schoolcategory.State == CommunicationState.Opened)
        //    {
        //        schoolcategory.Close();
        //    }
        //}
        //Select All options

        public void PopulateMLUI()
        {
            btnCancel.Text = DTL.Translate(btnCancel.Text, "en", langto);
            btnSave.Text = DTL.Translate(btnSave.Text, "en", langto);
            ltrlAddcat.Text = DTL.Translate(ltrlAddcat.Text, "en", langto);
            Literal1.Text = DTL.Translate(Literal1.Text, "en", langto);
            Literal2.Text = DTL.Translate(Literal2.Text, "en", langto);
            Literal3.Text = DTL.Translate(Literal3.Text, "en", langto);
            Literal4.Text = DTL.Translate(Literal4.Text, "en", langto);
            ML_0035.Text = DTL.Translate(ML_0035.Text, "en", langto);
            ML_0036.Text = DTL.Translate(ML_0036.Text, "en", langto);
            //ML_0039.Text = DTL.Translate(ML_0039.Text, "en", langto);
            //ML_0042.Text = DTL.Translate(ML_0042.Text, "en", langto);
            btnUpdate.Text = DTL.Translate(btnUpdate.Text, "en", langto);
            btnCancelEdit.Text = DTL.Translate(btnCancelEdit.Text, "en", langto);
            btnok.Text = DTL.Translate(btnok.Text, "en", langto);
            //chkDatadisplay.Text = DTL.Translate(chkDatadisplay.Text, "en", langto);
            //chkDataEntry.Text = DTL.Translate(chkDataEntry.Text, "en", langto);
            lblmsg.Text = DTL.Translate(lblmsg.Text, "en", langto);
            Literal12.Text = DTL.Translate(Literal12.Text, "en", langto);
            Literal5.Text = DTL.Translate(Literal5.Text, "en", langto);
            Literal6.Text = DTL.Translate(Literal6.Text, "en", langto);
            //Literal7.Text = DTL.Translate(Literal7.Text, "en", langto);
            //Literal8.Text = DTL.Translate(Literal8.Text, "en", langto);
            //Literal9.Text = DTL.Translate(Literal9.Text, "en", langto);
            //rfvFieldType.ErrorMessage = DTL.Translate(rfvFieldType.ErrorMessage, "en", langto);
            //rfvFieldName.ErrorMessage = DTL.Translate(rfvFieldName.ErrorMessage, "en", langto);
        }

        protected void gvSpecialEd_RowDataBound(object sender, GridViewRowEventArgs e)
        {            
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HiddenField hdnFieldType = (HiddenField)e.Row.FindControl("hdnFieldTypeSplId");
                    Label lblFieldType = (Label)e.Row.FindControl("lblFieldTypeSplId");
                    HiddenField hdnStatus = (HiddenField)e.Row.FindControl("hdnStatusSplId");
                    //Image ibtnOn = (Image)e.Row.FindControl("ibtnOnSplId");
                    //ImageButton ibtnOff = (ImageButton)e.Row.FindControl("ibtnOff");
                    Label lblFieldDisplaytype = (Label)e.Row.FindControl("lblFieldDisplaytypeSplId");
                    HiddenField hdnFieldId = (HiddenField)e.Row.FindControl("hdnFieldIdSplId");
                    HiddenField hdnFieldView = (HiddenField)e.Row.FindControl("hdnFieldViewSplId");
                    //Image ibtnDataEntryAddressFields = (Image)e.Row.FindControl("ibtnDataEntrySpecialFields");
                    //Image ibtnDataDisplayAddressFields = (Image)e.Row.FindControl("ibtnDataDisplaySpecialFields");
                    HiddenField hdnIsMandatory = (HiddenField)e.Row.FindControl("hdnIsMandatorySplId");
                    //ImageButton ibtnDelete = (ImageButton)e.Row.FindControl("ibtnDelete");
                    //if (hdnFieldView.Value.ToString().Trim() == "V")
                    //{
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "E")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "V_E")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_tick.png";
                    //}
                    //else if (hdnFieldView.Value.ToString().Trim() == "N" || hdnFieldView.Value.ToString().Trim() == "")
                    //{
                    //    ibtnDataEntryAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //    ibtnDataDisplayAddressFields.ImageUrl = "~/App_Themes/main/images/checkbox_notick.png";
                    //}
                    //if (hdnIsMandatory.Value.ToString() == "M")
                    //{
                    //    // ibtnDelete.Visible = false;
                    //    //ibtnOff.Attributes.Add("onclick", "return OpenModal('" + ModalPopupExtender3.ClientID + "');");
                    //}
                    //else
                    //{
                    //    // ibtnOff.Attributes.Add("onclick", " toolTip('')");
                    //}
                    Session["FieldId"] = Convert.ToInt32(hdnFieldId.Value);                    
                    //if (hdnStatus.Value.ToString() == "A")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //}
                    //else if (hdnStatus.Value.ToString() == "I")
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/Off.png";
                    //    ibtnDataEntryAddressFields.Visible = false;
                    //    ibtnDataDisplayAddressFields.Visible = false;
                    //}
                    //else
                    //{
                    //    ibtnOn.ImageUrl = "~/App_Themes/main/images/On.png";
                    //}
                    if (hdnFieldType.Value == "1")
                    {
                        lblFieldType.Text = "TextBox";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                    else if (hdnFieldType.Value == "2")
                    {
                        lblFieldType.Text = "CheckBox";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                    else if (hdnFieldType.Value == "3")
                    {
                        lblFieldType.Text = "RadioButton";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                    else if (hdnFieldType.Value == "4")
                    {
                        lblFieldType.Text = "DropDownList";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                    else if (hdnFieldType.Value == "5")
                    {
                        lblFieldType.Text = "Label";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                    else if (hdnFieldType.Value == "6")
                    {
                        lblFieldType.Text = "DatePicker";
                        //lblFieldDisplaytype.Text = "System Default";
                    }
                }
            }
            catch { }
            finally
            {
            }
        }

        protected void btnCategorySave_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceStudentSetupClient StudentSetupCTBL = new OpenSISWCFServiceStudentSetupClient();
            int SchoolId = Convert.ToInt32(Session["SchoolID"]);
            string CategoryName = txtCategoryName.Text.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;";
            StudentSetupCTBL.AddCategory(CategoryName, SchoolId);
            StudentSetupCTBL.Close();
            //populateStudentSetupCategory();
            string SchoolID = Session["SchoolID"].ToString();
            OpenSISWCFServiceStudentSetupClient OSISStudentClient = new OpenSISWCFServiceStudentSetupClient();
            DataTable dt = OSISStudentClient.GetSchoolSpecificCategory(SchoolID, Session["MLUICulture"].ToString());
            if (OSISStudentClient.State == CommunicationState.Opened)
            {
                OSISStudentClient.Close();
            }
            TreeView1.Nodes.Clear();
            string[,] YearNodes = new string[100, 3];
            for (int i = 1; i < dt.Rows.Count + 1; i++)
            {
                TreeNode TreeNodeCategory = new TreeNode();
                //if (i == 0)
                //{
                //    YearNodes[i, 0] = "+ Add Category&nbsp;&nbsp;&nbsp;&nbsp;";
                //    YearNodes[i, 1] = "0";
                //    YearNodes[i, 2] = "0";
                //}
                //else
                //{
                    YearNodes[i, 0] = dt.Rows[i - 1]["Cat_Name"].ToString();
                    YearNodes[i, 1] = dt.Rows[i - 1]["Cat_Id"].ToString();
                    YearNodes[i, 2] = dt.Rows[i - 1]["Short_Order"].ToString();
                //}
                TreeNodeCategory.Text = YearNodes[i, 0];
                TreeNodeCategory.Value = YearNodes[i, 1] + "," + i + "," + YearNodes[i, 2];
                hdnNode.Value = TreeNodeCategory.Value;
                hdnCatId.Value = YearNodes[i, 1];
                Session["cat_name"] = TreeNodeCategory.Text;
                hdnCatOrder.Value = YearNodes[i, 2];
                TreeView1.Nodes.Add(TreeNodeCategory);
                TreeView1.ExpandAll();                
            }
            OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
            int CategoryId = Convert.ToInt32(hdnCatId.Value);
         
            DataTable dtcat = schoolcategory.GetFieldsByCategory(CategoryId, SchoolId, Session["MLUICulture"].ToString());
            if (hdnCatId.Value == "3" || hdnCategoryId.Value == "3")
            {
                //DataTable dtadd = new DataTable();
                DataRow dr = null;
                //dt.Columns.Add("WeekEnd_Day");
                dr = dt.NewRow();
                dr["FieldName"] = "Additional Contact";
                dr["FieldId"] = "900";
                dr["FieldType"] = "0";
                dr["Status"] = "A";
                dr["Default_Custom"] = "AD";
                dr["IsMandatory"] = "M";
                dr["Field_View"] = "V_E";
                dtcat.Rows.Add(dr);
            }
            gvCategoryFields.DataSource = dtcat;
            gvCategoryFields.DataBind();
            lblHeader.Text = Session["cat_name"].ToString();
            lblShortOrder.Text = hdnCatOrder.Value;
            schoolcategory.Close();
            if (hdnNode.Value != "")
            {
                TreeView1.FindNode(hdnNode.Value.ToString()).Select();
            }
            //MView.ActiveViewIndex = 1;
            Session["cat_name"] = null;
            txtCategoryName.Text = "";
            panel1.Visible = false;

        }

        protected void ibtnEditCategory_Click(object sender, ImageClickEventArgs e)
        {  
            txtCatEdit.Text = lblHeader.Text.Replace("&nbsp;", "");
            //if (Session["CatSrtOrd"] != null)
            //{
            txtShortOrder.Text = lblShortOrder.Text;
            //}
            //else
            //{
            //    txtShortOrder.Text = hdnCatOrder.Value.ToString();
            //}           
            TreeView1.FindNode(hdnNode.Value.ToString()).Select();           
            ModalPopupExtender5.Enabled = true;
            ModalPopupExtender5.Show();
        }

        protected void btnCategoryEdit_Click(object sender, EventArgs e)
        {
            OpenSISWCFServiceStudentSetupClient StudentSetupBL = new OpenSISWCFServiceStudentSetupClient();
            string CategoryName = txtCatEdit.Text + "&nbsp;&nbsp;&nbsp;&nbsp;";
            string Cat_Order = txtShortOrder.Text;
            StudentSetupBL.UpdateCategoryNameAndOrder(int.Parse(hdnCatId.Value), int.Parse(hdnCatOrder.Value), CategoryName, int.Parse(txtShortOrder.Text));
            Session["CatSrtOrd"] = txtShortOrder.Text;
            //populateStudentSetupCategory();
            string SchoolID = Session["SchoolID"].ToString();
            OpenSISWCFServiceStudentSetupClient OSISStudentClient = new OpenSISWCFServiceStudentSetupClient();
            DataTable dt = OSISStudentClient.GetSchoolSpecificCategory(SchoolID, Session["MLUICulture"].ToString());
            if (OSISStudentClient.State == CommunicationState.Opened)
            {
                OSISStudentClient.Close();
            }
            TreeView1.Nodes.Clear();
            string[,] YearNodes = new string[100, 3];
            for (int i = 1; i < dt.Rows.Count + 1; i++)
            {
                TreeNode TreeNodeCategory = new TreeNode();
                //if (i == 0)
                //{
                //    YearNodes[i, 0] = "+ Add Category&nbsp;&nbsp;&nbsp;&nbsp;";
                //    YearNodes[i, 1] = "0";
                //    YearNodes[i, 2] = "0";
                //}
                //else
                //{
                    YearNodes[i, 0] = dt.Rows[i - 1]["Cat_Name"].ToString();
                    YearNodes[i, 1] = dt.Rows[i - 1]["Cat_Id"].ToString();
                    YearNodes[i, 2] = dt.Rows[i - 1]["Short_Order"].ToString();
                //}
                TreeNodeCategory.Text = YearNodes[i, 0];
                TreeNodeCategory.Value = YearNodes[i, 1] + "," + i + "," + YearNodes[i, 2];

                if (hdnCatId.Value == YearNodes[i, 1])
                {
                    hdnNode.Value = TreeNodeCategory.Value;
                    Session["cat_name"] = YearNodes[i, 0];
                    hdnCatId.Value = YearNodes[i, 1];
                }                
                hdnCatOrder.Value = YearNodes[i, 2];
                TreeView1.Nodes.Add(TreeNodeCategory);
                TreeView1.ExpandAll();
            }            
            OpenSISWCFServiceStudentSetupClient schoolcategory = new OpenSISWCFServiceStudentSetupClient();
            int CategoryId = Convert.ToInt32(hdnCatId.Value);
            DataTable dtcat = schoolcategory.GetFieldsByCategory(CategoryId,int.Parse(SchoolID), Session["MLUICulture"].ToString());
            if (hdnCatId.Value == "3" || hdnCategoryId.Value == "3")
            {
                //DataTable dtadd = new DataTable();
                DataRow dr = null;
                //dt.Columns.Add("WeekEnd_Day");
                dr = dtcat.NewRow();
                dr["FieldName"] = "Additional Contact";
                dr["FieldId"] = "900";
                dr["FieldType"] = "0";
                dr["Status"] = "A";
                dr["Default_Custom"] = "AD";
                dr["IsMandatory"] = "M";
                dr["Field_View"] = "V_E";
                dtcat.Rows.Add(dr);
            }
            gvCategoryFields.DataSource = dtcat;
            gvCategoryFields.DataBind();
            lblHeader.Text = Session["cat_name"].ToString();
            lblShortOrder.Text = Cat_Order;
            schoolcategory.Close();
            if (hdnNode.Value != "")
            {
                TreeView1.FindNode(hdnNode.Value.ToString()).Select();
            }
            StudentSetupBL.Close();
            ModalPopupExtender5.Hide();
            txtCategoryName.Text = "";
            txtShortOrder.Text = "";
        }

        protected void lnkAddCat_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        protected void btnCategoryCancel_Click(object sender, EventArgs e)
        {
            txtCategoryName.Text = "";
            panel1.Visible = false;
        }

        protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (TreeView1.SelectedNode.Value.Split(",".ToCharArray())[0] == "8")
            {
                foreach (GridViewRow Crw in gvCategoryFields.Rows)
                {
                    CheckBox chkrowCatFld = (CheckBox)Crw.FindControl("chkrowCatFld");
                    if (chkrowCatFld.Checked == true)
                    {
                        HiddenField hdnFieldIdCatFld = (HiddenField)Crw.FindControl("hdnFieldIdCatFld");
                        Label lblFieldNameCatFld = (Label)Crw.FindControl("lblFieldNameCatFld");
                        lblHeaderText.Text = lblFieldNameCatFld.Text;
                        lblHeaderText.Visible = true;
                        LitHeaderName.Visible = true;
                    }
                }
            }
            lblAddCategoryName.Text = TreeView1.SelectedNode.Text.ToString();
            ModalPopupExtender1.Enabled = true;
            ModalPopupExtender1.Show();
        }
    }
}